package com.liujit.pay.wx.domain.params.refunds;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;

/**
 * @description: 商品列表
 * @author: liujun
 * @create: 2021-02-20 09:57
 */
public class RefundsGoodsDetail {

    /**
     * 商户侧商品编码
     */
    private String merchantGoodsId;

    /**
     * 微信侧商品编码
     */
    private String wechatPayGoodsId;

    /**
     * 商品名称
     */
    private String goodsName;

    /**
     * 商品单价
     */
    private Integer unitPrice;

    /**
     * 商品退款金额
     */
    private Integer refundAmount;

    /**
     * 商品退货数量
     */
    private Integer refundQuantity;

    RefundsGoodsDetail(String merchantGoodsId, String wechatPayGoodsId, String goodsName, Integer unitPrice, Integer refundAmount, Integer refundQuantity) {
        this.merchantGoodsId = merchantGoodsId;
        this.wechatPayGoodsId = wechatPayGoodsId;
        this.goodsName = goodsName;
        this.unitPrice = unitPrice;
        this.refundAmount = refundAmount;
        this.refundQuantity = refundQuantity;
    }

    /**
     * 初始化参数
     *
     * @param merchantGoodsId 商户侧商品编码
     * @param unitPrice       商品单价
     * @param refundAmount    商品退款金额
     * @param refundQuantity  商品退货数量
     * @return
     */
    public static RefundsGoodsDetail.Builder builder(String merchantGoodsId, Integer unitPrice, Integer refundAmount, Integer refundQuantity) {
        return new RefundsGoodsDetail.Builder(merchantGoodsId, unitPrice, refundAmount, refundQuantity);
    }

    public static class Builder {

        private String merchantGoodsId;
        private String wechatPayGoodsId;
        private String goodsName;
        private Integer unitPrice;
        private Integer refundAmount;
        private Integer refundQuantity;

        Builder(String merchantGoodsId, Integer unitPrice, Integer refundAmount, Integer refundQuantity) {
            this.merchantGoodsId = merchantGoodsId;
            this.unitPrice = unitPrice;
            this.refundAmount = refundAmount;
            this.refundQuantity = refundQuantity;
        }

        /**
         * 设置微信侧商品编码
         *
         * @param wechatPayGoodsId 微信侧商品编码
         * @return
         */
        public RefundsGoodsDetail.Builder wechatPayGoodsId(String wechatPayGoodsId) {
            this.wechatPayGoodsId = wechatPayGoodsId;
            return this;
        }

        /**
         * 设置商品名称
         *
         * @param goodsName 商品名称
         * @return
         */
        public RefundsGoodsDetail.Builder goodsName(String goodsName) {
            this.goodsName = goodsName;
            return this;
        }

        public RefundsGoodsDetail build() {
            return new RefundsGoodsDetail(this.merchantGoodsId, this.wechatPayGoodsId, this.goodsName, this.unitPrice, this.refundAmount, this.refundQuantity);
        }
    }

    /**
     * 转化JSON字符串
     *
     * @return
     */
    public JSONObject toJSONObject() {
        JSONObject json = new JSONObject();
        // 商户侧商品编码
        json.set("merchant_goods_id", merchantGoodsId);
        // 微信侧商品编码
        if (StrUtil.isNotBlank(wechatPayGoodsId)) {
            json.set("wechatpay_goods_id", wechatPayGoodsId);
        }
        // 商品名称
        if (StrUtil.isNotBlank(goodsName)) {
            json.set("goods_name", goodsName);
        }
        // 商品单价
        json.set("unit_price", unitPrice);
        // 商品退款金额
        json.set("refund_amount", refundAmount);
        // 商品退货数量
        json.set("refund_quantity", refundQuantity);
        return json;
    }
}
